/**
 * Created by colin on 2017/5/18.
 */

$(function () {
    $('.delp').click(function () {
        var pid = $(this).attr('pid');
        var title = $('#title').val();
        confirm("确定要删除该订单吗?", "", function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    url:'/delper.action',
                    type:"post",
                    data:{
                        perId:pid
                    },
                    dataType:'json',
                    success:function (data) {
                        var obj = eval('('+data+')');
                        if (obj.status===1){
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'success', confirmButtonText: '确定'});
                        } else {
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'error', confirmButtonText: '确定'});
                        }
                    }
                });
            }
        }, {type: 'warning',confirmButtonText: '确定', cancelButtonText: '取消'});

    });

    $('.done').click(function () {
        var pid = $(this).attr('pid');
        var title = $('#title').val();
        confirm("确定收到货物了吗?", "", function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    url:'/perdone.action',
                    type:"post",
                    data:{
                        perId:pid
                    },
                    dataType:'json',
                    success:function (data) {
                        var obj = eval('('+data+')');
                        if (obj.status===1){
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'success', confirmButtonText: '确定'});
                        } else {
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'error', confirmButtonText: '确定'});
                        }
                    }
                });
            }
        }, {type: 'question',confirmButtonText: '确定', cancelButtonText: '取消'});

    });

    $('.pay').click(function () {
        var purchaseId = $(this).attr('pid');
        confirm("确定支付吗?", "", function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    url:'/payI',
                    type:"post",
                    data:{
                        purchaseId:purchaseId
                    },
                    dataType:'json',
                    success:function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.status === 1) {
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/pay/paylist';
                            }, {type: 'success', confirmButtonText: '确定'});
                        } else {
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/waits';
                            }, {type: 'error', confirmButtonText: '确定'});
                        }
                    }
                });

            }
        }, {type: 'question',confirmButtonText: '确定', cancelButtonText: '取消'});

    });

    $('.goods-detail').click(function () {
        var carId = $(this).attr('carId');
        var perId = $(this).attr('perId');
        var flag = $(this).attr('flag');
        $.ajax({
            url:'/goodsdetail.action',
            type:"post",
            data:{
                carId:carId
            },
            dataType:'json',
            success:function (data) {
                var obj = eval('('+data+')');
                $("#perId").val(perId);
                $("#goodsId").val(obj.goodsId);
                $("#bgoodsid").val(obj.goodsId);
                $("#gname").val(obj.name);
                $("#description").val(obj.describ);
                $("#price").val(obj.actprice);
                $("#num").val(obj.numbers);
                $("#bnum").val(obj.numbers);
                $("#total").val(obj.sumprice);
                $("#photo").attr('src',obj.photo);
                $("#photo-a").attr('href',obj.photo);
                if (flag === '4'){
                    $('#apply').removeClass('hidden');
                }else {
                    $('#apply').addClass('hidden');
                }
            }
        });
    });

    $('#btnSub').on('click',function () {
        var perId = $("#perId").val();
        var goodId = $("#goodsId").val();
        var oldNum = $("#num").val();
        var bgId = $("#bgoodsid").val();
        var newNum = $("#bnum").val();
        var title = $('#title').val();
        var gtype = $("input[name=gtype]:checked").val();
        var reson = $('#reason').val();
        var params = $.param({
            'perId':perId,
            'goodId':goodId,
            'oldNum':oldNum,
            'gtype':gtype,
            'bgoodId':bgId,
            'newNum':newNum,
            'reson':reson
        },true);
        confirm("确定退换货申请吗?", "", function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    url:'/returnPolicy.action',
                    type:"post",
                    data:params,
                    dataType:'json',
                    success:function (data) {
                        var obj = eval('('+data+')');
                        if (obj.status===1){
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'success', confirmButtonText: '确定'});
                        } else {
                            alert(obj.msg, "", function () {
                                window.location.href = '/front/purchase/'+title;
                            }, {type: 'error', confirmButtonText: '确定'});
                        }
                    }
                });
            }
        }, {type:'question',confirmButtonText:'确定', cancelButtonText:'取消'});


    });

    $('#detail').on('hide.bs.modal', function () {
        $('div .return').each(function () {
            $(this).addClass('hidden');
        });
        $('div .change').each(function () {
            $(this).addClass('hidden');
        });
        $('#btnSub').addClass('hidden');
    });

    $('#apply').click(function () {
        $('div .return').each(function () {
           $(this).removeClass('hidden');
        });
        $('#btnSub').removeClass('hidden');
        $(this).addClass('hidden');
    });

    $('#return').click(function () {
        $('div .change').each(function () {
            $(this).addClass('hidden');
        });
    });

    $('#change').click(function () {
        $('div .change').each(function () {
            $(this).removeClass('hidden');
        });
    });
});